package com.project.webpage.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.project.webpage.dto.*;
import com.project.webpage.mapper.LdxxMapper;
import com.project.webpage.mapper.SkbDailyMapper;
import com.project.webpage.service.LdxxService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class LdxxServiceImpl implements LdxxService {

    @Resource
    private LdxxMapper ldxxMapper;
    @Resource
    private SkbDailyMapper skbDailyMapper;

    @Override
    @DS("master")
    public List<GpsCphDto> getPcListByJgxlidForPage(SearchByRqParam param) {
        return ldxxMapper.selectPcListByJgxlid(param);
    }

    @Override
    @DS("master")
    public GpsBcListDto getBcListByJgxlidForPage(SearchByRqParam param) {
        GpsBcListDto gpsBcListDto = new GpsBcListDto();
        gpsBcListDto.setType("QY");
        List<GpsBcDto> list = new ArrayList<>();
        List<GpsBcDto> resultList = new ArrayList<>();
        // 时刻表班次
        List<SkbDetailDto> skbDetailDtoList = skbDailyMapper.selectDetailListByJgxlid(param);
        if (skbDetailDtoList.size() > 0) {
            for (SkbDetailDto skbDetailDto : skbDetailDtoList) {
                GpsBcDto dto = new GpsBcDto();
                dto.setSkbDetail(skbDetailDto);
                list.add(dto);
                resultList.add(dto);
            }
        }
        List<LdxxBcDto> ldxxBcDtoList = ldxxMapper.selectBcListByJgxlid(param);
        if (ldxxBcDtoList.size() > 0) {
            if (list.size() > 0) {
                for (int i = 0; i < list.size(); i++) {
                    SkbDetailDto skbDetailDto = list.get(i).getSkbDetail();
                    List<LdxxBcDto> ldxxBcDtos = ldxxBcDtoList.stream().filter(item -> item.getSxx_int() == skbDetailDto.getSxx_int() && item.getJhfcsj().equals(skbDetailDto.getJhfcsj())).collect(Collectors.toList());
                    if (ldxxBcDtos.size() > 0) {
                        resultList.get(i).setBcDetail(ldxxBcDtos.get(0));
                        ldxxBcDtos.get(0).setIscheck(1);
                        if (ldxxBcDtos.size() > 1) {
                            for (int j = 1; j < ldxxBcDtos.size(); j++) {
                                GpsBcDto dto = new GpsBcDto();
                                dto.setBcDetail(ldxxBcDtos.get(j));
                                if (i == list.size() - 1) {
                                    resultList.add(dto);
                                } else {
                                    resultList.add(i + 1, dto);
                                }
                                ldxxBcDtos.get(j).setIscheck(1);
                            }
                        }
                    }
                }
                // 未匹配数据重新匹配
                List<LdxxBcDto> ldxxBcDtoNochecks = ldxxBcDtoList.stream().filter(item -> item.getIscheck() == null).collect(Collectors.toList());
                if (ldxxBcDtoNochecks.size() > 0) {
                    List<GpsBcDto> newList = new ArrayList<>();
                    for (int i = 0; i < ldxxBcDtoNochecks.size(); i++) {
                        long jhfcTimeNochecks = ldxxBcDtoNochecks.get(i).getJhfcsj_time().getTime();
                        for (int a = 0; a < resultList.size(); a++) {
                            long jhfcTime = resultList.get(a).getSkbDetail().getJhfcsj_time().getTime();
                            long diffInMis = (jhfcTimeNochecks - jhfcTime) / (1000 * 60);
                            if (resultList.get(a).getBcDetail() == null && diffInMis >= -15 && diffInMis <= 15) {
                                resultList.get(a).setBcDetail(ldxxBcDtoNochecks.get(i));
                                ldxxBcDtoNochecks.get(i).setIscheck(1);
                                break;
                            }
                        }
                        if (ldxxBcDtoNochecks.get(i).getIscheck() == null) {
                            if (newList.size() == 0) {
                                Collections.copy(newList, resultList);
                            }
                            GpsBcDto dto = new GpsBcDto();
                            dto.setBcDetail(ldxxBcDtoNochecks.get(i));
                            for (int n = 0; n < newList.size(); n++) {
                                long jhfcTime = newList.get(n).getBcDetail().getJhfcsj_time().getTime();
                                long diffInMis = (jhfcTimeNochecks - jhfcTime) / (1000 * 60);
                                if (diffInMis >= -15 && diffInMis <= 15) {
                                    if (n == newList.size() - 1) {
                                        newList.add(dto);
                                    } else {
                                        newList.add(n + 1, dto);
                                    }
                                    dto.getBcDetail().setIscheck(1);
                                    break;
                                }
                            }
                            if (dto.getBcDetail().getIscheck() == null) {
                                newList.add(dto);
                                dto.getBcDetail().setIscheck(1);
                            }
                        }
                    }
                    if (newList.size() > 0) {
                        Collections.copy(resultList, newList);
                    }
                }
            } else {
                for (LdxxBcDto ldxxBcDto : ldxxBcDtoList) {
                    GpsBcDto dto = new GpsBcDto();
                    dto.setBcDetail(ldxxBcDto);
                    resultList.add(dto);
                }
            }
        }
        gpsBcListDto.setBcList(resultList);
        // 区间班次
        List<LdxxBcDto> ldxxQjBcDtoList = ldxxMapper.selectQjBcListByJgxlid(param);
        gpsBcListDto.setQjBcList(ldxxQjBcDtoList);
        return gpsBcListDto;
    }
}
